package se.webbzon.oschi01.gui;

import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

import se.webbzon.oschi01.action.NBTCopyAction;
import se.webbzon.oschi01.action.NBTDeleteAction;
import se.webbzon.oschi01.action.NBTNewAction;
import se.webbzon.oschi01.action.NBTOpenAction;
import se.webbzon.oschi01.action.NBTPasteAction;
import se.webbzon.oschi01.action.NBTRenameAction;
import se.webbzon.oschi01.command.CommanderAction;
import se.webbzon.oschi01.nbt.ByteArrayTagFactory;
import se.webbzon.oschi01.nbt.ByteTagFactory;
import se.webbzon.oschi01.nbt.CompoundTagFactory;
import se.webbzon.oschi01.nbt.DoubleTagFactory;
import se.webbzon.oschi01.nbt.FloatTagFactory;
import se.webbzon.oschi01.nbt.IntArrayTagFactory;
import se.webbzon.oschi01.nbt.IntTagFactory;
import se.webbzon.oschi01.nbt.ListTagFactory;
import se.webbzon.oschi01.nbt.LongTagFactory;
import se.webbzon.oschi01.nbt.ShortTagFactory;
import se.webbzon.oschi01.nbt.StringTagFactory;
import se.webbzon.oschi01.nbt.Tag;
import se.webbzon.oschi01.nbt.TagFactory;
import se.webbzon.oschi01.singleton.Singletons;

public class NBTPopupMenu extends JPopupMenu {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 8225269636615311873L;
	
	private final ApplicationIconSheet iconSheet = Singletons.instance(ApplicationIconSheet.class);

	public NBTPopupMenu(Tag<?> nbtTag, MainModel model) {
		// Add "new" menu 
		JMenu newMenu = new JMenu(new NewAction());
		TagFactory<?>[] nbtFactories = {	
				Singletons.instance(ByteTagFactory.class),
				Singletons.instance(ShortTagFactory.class),
				Singletons.instance(IntTagFactory.class),
				Singletons.instance(LongTagFactory.class),
				Singletons.instance(FloatTagFactory.class),
				Singletons.instance(DoubleTagFactory.class),
				Singletons.instance(ByteArrayTagFactory.class),
				Singletons.instance(StringTagFactory.class),
				Singletons.instance(ListTagFactory.class),
				Singletons.instance(CompoundTagFactory.class),
				Singletons.instance(IntArrayTagFactory.class)
		};
		for (TagFactory<?> nbtFactory : nbtFactories) {
			NBTNewAction action = new NBTNewAction(this,nbtTag,nbtFactory);
			action.setListener(model.getHistory());
			newMenu.add(action);
		}
		add(newMenu);
		
		add(new JMenuItem(new NBTOpenAction(nbtTag)));
		addSeparator();
		
		// Add some actions
		CommanderAction[] nbtActions = {	
				new NBTCopyAction(nbtTag,model.getClipboard()),
				new NBTPasteAction(this,nbtTag,model.getClipboard()),
				new NBTDeleteAction(nbtTag)
		};
		
		for (CommanderAction action : nbtActions) {
			action.setListener(model.getHistory());
			add(action);
		}
		addSeparator();
		
		CommanderAction action = new NBTRenameAction(this,nbtTag);
		action.setListener(model.getHistory());
		add(new JMenuItem(action));
	}
	
	private final class NewAction extends AbstractAction {
		
		/**
		 * 
		 */
		private static final long serialVersionUID = 4741234631012276150L;

		public NewAction() {
			putValue(NAME, "New");
			putValue(SMALL_ICON, iconSheet.getIcon(ApplicationIcon.New));
		}

		@Override public void actionPerformed(ActionEvent e) {};
		
	}

}
